package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.*;
import com.eastfair.venueservice.entity.ServiceInventoryOutFlow;
import com.eastfair.venueservice.service.ServiceInventoryOutFlowService;
import com.eastfair.venueservice.vo.ServiceInventoryOutFlowVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 服务库存出库流水记录
 * </p>
 *
 * @author linan
 * @date 2022-09-19
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/serviceInventoryOutFlow")
@Api(value = "ServiceInventoryOutFlow", tags = "服务库存出库流水记录")
@PreAuth(replace = "venueservice:serviceInventoryOutFlow:")
public class ServiceInventoryOutFlowController extends SuperController<ServiceInventoryOutFlowService, Long, ServiceInventoryOutFlow, ServiceInventoryOutFlowPageQuery, ServiceInventoryOutFlowSaveDTO, ServiceInventoryOutFlowUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<ServiceInventoryOutFlow> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ServiceInventoryOutFlow> serviceInventoryOutFlowList = list.stream().map((map) -> {
            ServiceInventoryOutFlow serviceInventoryOutFlow = ServiceInventoryOutFlow.builder().build();
            //TODO 请在这里完成转换
            return serviceInventoryOutFlow;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(serviceInventoryOutFlowList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ServiceInventoryOutFlowPageQuery> params) {
        ServiceInventoryOutFlowPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }


    /**
     * 插入出库流水
     * @param serviceInventoryOutFlowDTO
     * @return R<Boolean>
     */
    @ApiOperation(value = "插入出库流水")
    @SysLog("出库")
    @PostMapping(value = "/saveOutFlow")
    public R<Boolean> saveOutFlow(@RequestBody ServiceInventoryOutFlowDTO serviceInventoryOutFlowDTO) {
        try {
            return R.success(baseService.saveOutFlow(serviceInventoryOutFlowDTO));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 查询出库记录
     * @param serviceInventoryOutFlowQuery
     * @return
     */
    @ApiOperation(value = "查询出库记录")
    @PostMapping(value = "/queryOutFlowList")
    public R<Page<ServiceInventoryOutFlowVO>> queryOutFlowList(@RequestBody PageParams<ServiceInventoryOutFlowQuery> serviceInventoryOutFlowQuery){
        try {
            return R.success(baseService.queryOutFlowList(serviceInventoryOutFlowQuery));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 统计主场服务分类出库量.
     *
     * @param exhibitionManageId the exhibition manage id
     * @param homeId             the home id
     * @param topN               the top n
     * @return the r
     */
    @GetMapping("/count/service/category/outNum")
    public R<List<Map<String, Object>>> countServiceCategoryOutFlow(@RequestParam("exhibitionManageId") Long exhibitionManageId,
                                                                    @RequestParam("homeId") Long homeId,
                                                                    @RequestParam("topN") Integer topN) {
        log.info("countServiceCategoryOutFlow - 统计主场服务分类出库量, exhibitionManageId={}, homeId={}, topN={}", exhibitionManageId, homeId, topN);
        return R.successDef(baseService.countServiceCategoryOutFlow(exhibitionManageId, homeId, topN));
    }

    /**
     * 统计主场服务出库量.
     *
     * @param exhibitionManageId the exhibition manage id
     * @param homeId             the home id
     * @param topN               the top n
     * @return the r
     */
    @GetMapping("/count/service/outNum")
    public R<List<Map<String, Object>>> countServiceOutFlow(@RequestParam("exhibitionManageId") Long exhibitionManageId,
                                                            @RequestParam("homeId") Long homeId,
                                                            @RequestParam("topN") Integer topN) {
        log.info("countServiceOutFlow - 统计主场服务出库量, exhibitionManageId={}, homeId={}, topN={}", exhibitionManageId, homeId, topN);
        return R.successDef(baseService.countServiceOutFlow(exhibitionManageId, homeId, topN));
    }

    /**
     * 导出服务分类出库量.
     *
     * @param exhibitionManageId the exhibition manage id
     * @param homeId             the home id
     * @param response           the response
     * @throws IOException the io exception
     */
    @GetMapping("/export/service/category/outNum")
    public void exportServiceCategoryOutFlow(@RequestParam("exhibitionManageId") Long exhibitionManageId,
                                             @RequestParam("homeId") Long homeId,
                                             HttpServletResponse response) throws IOException {
        log.info("exportServiceCategoryOutFlow - 导出服务分类出库量, exhibitionManageId={}, homeId={}", exhibitionManageId, homeId);
        baseService.exportServiceCategoryOutFlow(exhibitionManageId, homeId, response);
    }

    /**
     * 导出服务出库量.
     *
     * @param exhibitionManageId the exhibition manage id
     * @param homeId             the home id
     * @param response           the response
     * @throws IOException the io exception
     */
    @GetMapping("/export/service/outNum")
    public void exportServiceOutFlow(@RequestParam("exhibitionManageId") Long exhibitionManageId,
                                     @RequestParam("homeId") Long homeId,
                                     HttpServletResponse response) throws IOException {
        log.info("exportServiceOutFlow - 导出服务出库量, exhibitionManageId={}, homeId={}", exhibitionManageId, homeId);
        baseService.exportServiceOutFlow(exhibitionManageId, homeId, response);
    }
}
